<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="接单技师" prop="userId">
              <el-input v-model="queryParams.userId" placeholder="请输入接单技师" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="下单用户" prop="appUserId">
              <el-input v-model="queryParams.appUserId" placeholder="请输入下单用户" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="套餐" prop="menuIds">
              <el-input v-model="queryParams.menuIds" placeholder="请输入套餐" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="服务时间" prop="serveDate">
              <el-date-picker clearable
                v-model="queryParams.serveDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择服务时间"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:order:add']">新增</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:order:edit']">修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:order:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:order:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="接单技师" align="center" prop="userName" />
        <el-table-column label="下单用户" align="center" prop="appUserName" />
        <el-table-column label="订单号" align="center" prop="orderNo" />
        <el-table-column label="套餐名称" align="center" prop="serviceName" />
        <el-table-column label="服务方式" align="center" prop="serveType" />
        <el-table-column label="健康类型" align="center" prop="healthName" />
        <el-table-column label="订单费用" align="center" prop="orderPrice" />
        <el-table-column label="服务时间" align="center" prop="serveDate" />
        <el-table-column label="状态" align="center" prop="status" >
          <template #default="scope">
            {{ scope.row.status==0?"预约下单":scope.row.status==1?"待服务":scope.row.status==2?"服务中":scope.row.status==3?"已完成":scope.row.status==4?"撤销":"" }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="留言" align="center" prop="leaveMessage" /> -->
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-if="scope.row.status==0" link type="primary"  @click="handleUpdateHx(scope.row)" >订单核销</el-button>
            <el-button v-if="scope.row.status==2" link type="primary"  @click="handleUpdateEnd(scope.row)" >技师结束</el-button>
            <!-- <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:order:edit']"></el-button>
            </el-tooltip> -->
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:order:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接单技师" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入接单技师" />
        </el-form-item>
        <el-form-item label="下单用户" prop="appUserId">
          <el-input v-model="form.appUserId" placeholder="请输入下单用户" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="套餐" prop="menuIds">
            <el-input v-model="form.menuIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单费用" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单费用" />
        </el-form-item>
        <el-form-item label="服务时间" prop="serveDate">
          <el-input v-model="form.serveDate" placeholder="请输入服务时间" />
        </el-form-item>
           <el-form-item label="包厢号" prop="serveRoom">
          <el-input v-model="form.serveRoom" placeholder="请输入服务时间" />
        </el-form-item>
           <el-form-item label="茶水" prop="serveTea">
          <el-input v-model="form.serveTea" placeholder="请输入服务时间" />
        </el-form-item>
        <el-form-item label="留言" prop="leaveMessage">
          <el-input v-model="form.leaveMessage" placeholder="请输入留言" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

     <!-- 订单核销修改订单对话框 -->
    <el-dialog :title="dialogHx.title" v-model="dialogHx.visible" width="500px" append-to-body>
      <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="核销码" prop="verificationCode">
          <el-input v-model="form.verificationCode" placeholder="请输入核销码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitFormHx">确 定</el-button>
          <el-button @click="cancelHx">取 消</el-button>
        </div>
      </template>
    </el-dialog>

       <!-- 技师结束订单订单对话框 -->
    <el-dialog :title="dialogEnd.title" v-model="dialogEnd.visible" width="500px" append-to-body>
      <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入核销码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitFormEnd">确 定</el-button>
          <el-button @click="cancelEnd">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order" lang="ts">
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,verificationCode,endOrder} from '@/api/system/order';
import { OrderVO, OrderQuery, OrderForm } from '@/api/system/order/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const orderList = ref<OrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const statusList = ref([
{lable:'预约下单',value:'0'},
{lable:'待服务',value:'1'},
{lable:'服务中',value:'2'},
{lable:'已完成',value:'3'},
{lable:'撤销',value:'4'}]);

const queryFormRef = ref<ElFormInstance>();
const orderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const dialogHx = reactive<DialogOption>({
  visible: false,
  title: ''
});


const dialogEnd = reactive<DialogOption>({
  visible: false,
  title: ''
});


const initFormData: OrderForm = {
  orderId: undefined,
  userId: undefined,
  appUserId: undefined,
  orderNo: undefined,
  menuIds: undefined,
  orderPrice: undefined,
  serveType: undefined,
  serveDate: undefined,
  leaveMessage: undefined,
  remark: undefined,
  status: undefined,
}
const data = reactive<PageData<OrderForm, OrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    appUserId: undefined,
    orderNo: undefined,
    menuIds: undefined,
    serveType: undefined,
    serveDate: undefined,
    params: {
    }
  },
  rules: {
    orderId: [
      { required: true, message: "订单id不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "接单技师不能为空", trigger: "blur" }
    ],
    appUserId: [
      { required: true, message: "下单用户不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    serveType: [
      { required: true, message: "服务方式不能为空", trigger: "change" }
    ],
    serveDate: [
      { required: true, message: "服务时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const getStatus = (row) =>{
  if(row==0){
   
  }
}

/** 查询订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrder(queryParams.value);
  orderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  orderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrderVO[]) => {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加订单";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: OrderVO) => {
  reset();
  const _orderId = row?.orderId || ids.value[0]
  const res = await getOrder(_orderId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改订单";
}


/** 核销按钮操作 */
const handleUpdateHx = async (row?: OrderVO) => {
  reset();
  const _orderId = row?.orderId || ids.value[0]
  form.value.orderId=_orderId
  dialogHx.visible = true;
  dialogHx.title = "订单核销";
}
/** 取消核销按钮 */
const cancelHx = () => {
  reset();
  dialogHx.visible = false;
}

/** 技师结束订单按钮操作 */
const handleUpdateEnd = async (row?: OrderVO) => {
  reset();
  const _orderId = row?.orderId || ids.value[0]
  const res = await getOrder(_orderId);
  form.value.orderId=_orderId
  form.value.orderNo=res.data.orderNo
  form.value.status='3';
  dialogEnd.visible = true;
  dialogEnd.title = "是否结束订单";
}
/** 取消核销按钮 */
const cancelEnd = () => {
  reset();
  dialogEnd.visible = false;
}

/** 提交按钮 */
const submitForm = () => {
  orderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.orderId) {
        await updateOrder(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addOrder(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}


/** 核销提交按钮 */
const submitFormHx = async () => {
      buttonLoading.value = true;
      await verificationCode(form.value).finally(() =>  buttonLoading.value = false);
      proxy?.$modal.msgSuccess("核销成功");
      dialogHx.visible = false;
      await getList();
}

/** 技师结束订单提交按钮 */
const submitFormEnd = async () => {
      buttonLoading.value = true;
      await endOrder(form.value).finally(() =>  buttonLoading.value = false);
      proxy?.$modal.msgSuccess("结束成功");
      dialogEnd.visible = false;
      await getList();
}
 
 

/** 删除按钮操作 */
const handleDelete = async (row?: OrderVO) => {
  const _orderIds = row?.orderId || ids.value;
  await proxy?.$modal.confirm('是否确认删除订单编号为"' + _orderIds + '"的数据项？').finally(() => loading.value = false);
  await delOrder(_orderIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
